#!/bin/bash
# * =====================================================
# * Copyright © hk. 2022-2025. All rights reserved.
# * File name  : net-tools.sh
# * Author     : 苏木
# * Date       : 2025-10-19
# * Version    : 
# * Description: 
# * ======================================================
##

# 脚本和工程路径
# ========================================================
SCRIPT_NAME=${0#*/}
SCRIPT_CURRENT_PATH=${0%/*}
SCRIPT_ABSOLUTE_PATH=`cd $(dirname ${0}); pwd`
PROJECT_ROOT=${SCRIPT_ABSOLUTE_PATH}/.. # 工程的源码目录，一定要和编译脚本是同一个目录

source "$PROJECT_ROOT/common/common.sh"

# ========================================================
# 功能实现
# ========================================================
EXECUTE_MODE=manu
function usage()
{
	prt "================================================="
    prt "${PINK}./${SCRIPT_NAME}      : 根据菜单选择功能${CLS}"
    prt "${PINK}./${SCRIPT_NAME} -h   : 显示打印信息用法后显示菜单${CLS}"
    prt "${PINK}./${SCRIPT_NAME} -a   : 自动执行所有步骤${CLS}"
    prt "================================================="
}
# 脚本运行参数处理
notice "There are $# parameters: $@ (\$1~\$$#)"
while getopts "ahp:" arg #选项后面的冒号表示该选项需要参数
    do
        case ${arg} in
            a) EXECUTE_MODE="auto" ;;
            h)
                log_echo_demo
                ;;
            p)
                if [ $OPTARG == "1" ];then
                    message "OPTARG=$OPTARG"
                fi
                ;;
            ?)  #当有不认识的选项的时候arg为?
                error "unkonw argument..."
                exit 1
                ;;
        esac
    done

# 功能函数实现
# ========================================================
# 安装 net-tools 
function net_tools_install()
{
    cdi ~
    prt "${PINK}current path         :$(pwd)${CLS}"

	if ! $(command -v ifconfig >/dev/null 2>&1) ; then
		warning "net-tools 尚未安装,准备在线安装net-tools..."
        execute apt-get update
        execute apt install -y net-tools
    fi
    info "net-tools 已安装,本机IP信息如下:"
	ifconfig
    cdo
}

# 解决网络图标消失问题(网卡消失)，这个在虚拟机中有时候会出现
function net_icon_solution()
{
    execute service network-manager stop
    execute rm /var/lib/NetworkManager/NetworkManager.state
    execute service network-manager start
}

# 更更换软件源
function source_list_change()
{
    cdi ${SCRIPT_ABSOLUTE_PATH}
    prt "${PINK}current path         :$(pwd)${CLS}"

    info "更换软件源为阿里云软件源..."

    if [ -f "./etc/apt/source-focal-aliyun.list" ]; then
        execute cp -pvf ./etc/apt/source-focal-aliyun.list /etc/apt/sources.list
        execute apt-get update
        success "更换完毕..."
    else
        echo "文件 ./etc/apt/source-focal-aliyun.list 不存在"
    fi
}

# 自动执行所有步骤
function auto_setup() 
{
    source_list_change
    net_tools_install
    success "所有步骤执行完成!"
}

# 菜单与主进程
# ========================================================
function show_menu()
{
    message "================================================="
	message "                script by @苏木    ${CLS}"
	message "================================================="
    prt "${PINK}current path         :$(pwd)${CLS}"
    prt "${PINK}PROJECT_ROOT         :${PROJECT_ROOT}${CLS}"
    prt "${PINK}SCRIPT_ABSOLUTE_PATH :${SCRIPT_ABSOLUTE_PATH}${CLS}"
    echo ""
    prt "* [0] 安装net-tools"
    prt "* [1] 更新软件源为阿里软件源"
    prt "* [2] 解决VMware中系统网络图标消失问题"
    echo "================================================="
}

# 主逻辑
function main() 
{
    if [ "$EXECUTE_MODE" = "manu" ]; then
        while true; do
            show_menu
            read -p "请选择操作 (0-5): " choice
            case "${choice}" in
                "0") net_tools_install;;
                "1") source_list_change;;
                "2") net_icon_solution;;
                *) error "无效选项!!!" ;;
            esac
            read -p "按回车继续..."
        done
    else
        info "正在自动执行所有配置步骤..."
        auto_setup
    fi
}

# 执行入口
# show_menu # 菜单显示
main "$@" # 主逻辑执行
